package com.example.seakdreamandroid.whiteNoise;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Base64;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.example.seakdreamandroid.Constant;
import com.example.seakdreamandroid.R;
import com.example.seakdreamandroid.accountInfo.AccountInfoActivity;
import com.example.seakdreamandroid.voiceCommunity.VoiceRemarkActivity;
import com.example.seakdreamandroid.whiteNoisePlayer.WhiteNoisePlayerActivity;
import com.example.seakdreamandroid.whiteNoisePlayer.WhiteNoisePlayerService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.io.Serializable;
import java.lang.reflect.Type;
import java.sql.Time;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class WhiteNoiseSleepFriendChannelAdapter extends BaseAdapter implements View.OnClickListener {
    //上下文环境
    private Context mContext;
    //数据源
    private List<WhiteNoiseSleepFriendChannel> channels;
    private Callback mCallback;
    private int selectedId = -1;

    public void setSelectedId(int selectedId) {
        this.selectedId = selectedId;
    }

    /**
     * 自定义接口，用于回调播放按钮点击事件到对应的fragment
     * @param
     * @param
     */
    public interface Callback{
        public void click(View v);
    }


    public WhiteNoiseSleepFriendChannelAdapter(Context mContext, List<WhiteNoiseSleepFriendChannel> channels
                ,Callback mCallback) {
        this.mContext = mContext;
        this.channels = channels;
        this.mCallback = mCallback;
    }
    //返回数据源中元素的数量
    @Override
    public int getCount() {
        return channels.size();
    }
    //返回数据源中指定序号的数据对象
    @Override
    public Object getItem(int position) {
        return channels.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public void onClick(View v) {
        mCallback.click(v);
    }

    private static class ViewHolder{
        //每一个item中的控制
        ImageView head,/*music1,music2,music3,*/playBackground,play;
        TextView name,time,content,count;
        LinearLayout linearLayout;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        ViewHolder viewHolder;
        if (view == null) {


            //加载item的布局文件，生成一个view对象，再给view赋值
            view = LayoutInflater.from(mContext).inflate(R.layout.item_whitenoise_sleepfriend, null);
            viewHolder = new ViewHolder();

            viewHolder.linearLayout = view.findViewById(R.id.sleep_friend_linearLayout);
            viewHolder.head = view.findViewById(R.id.sleep_friend_head);
            viewHolder.playBackground = view.findViewById(R.id.sleep_friend_play_background);
            viewHolder.name = view.findViewById(R.id.sleep_friend_name);
            viewHolder.time = view.findViewById(R.id.sleep_friend_time);
            viewHolder.content = view.findViewById(R.id.sleep_friend_content);
            viewHolder.count = view.findViewById(R.id.sleep_friend_count);
            viewHolder.play = view.findViewById(R.id.sleep_friend_play);
            //将viewHolder对象同view关联起来
            view.setTag(viewHolder);

        }else{
            //重复利用view对象
            //获取到跟当前view绑定在一起的viewholder对象
            viewHolder = (ViewHolder) view.getTag();


        }
        WhiteNoiseSleepFriendChannel channel = channels.get(position);


        RequestOptions requestOptions = new RequestOptions();
        byte[] bs = Base64.decode(channel.getHead(), Base64.DEFAULT);
        for (int i = 0; i < bs.length; ++i) {
            if (bs[i] < 0) {
                bs[i] += 256;
            }
        }
        Bitmap bitmap = BitmapFactory.decodeByteArray(bs,0,bs.length);

        Glide.with(mContext).load(bitmap).apply(RequestOptions.bitmapTransform(new CircleCrop())).into(viewHolder.head);

//        Glide.with(mContext)
//                .load(channel.getHead())
//                .apply(requestOptions)
//                .into(viewHolder.head);
        /*Glide.with(mContext)
                .load(channel.getMusic1())
                .apply(requestOptions)
                .into(viewHolder.music1);
        Glide.with(mContext)
                .load(channel.getMusic2())
                .apply(requestOptions)
                .into(viewHolder.music2);
        Glide.with(mContext)
                .load(channel.getMusic3())
                .apply(requestOptions)
                .into(viewHolder.music3);*/
        if(position % 2 == 0){
            //主背景色3eda9b
            viewHolder.linearLayout.setBackgroundResource(R.drawable.linearlayout_shape);
            //播放按钮背景
            viewHolder.playBackground.setBackgroundColor(Color.parseColor("#4fa080"));
            //音乐图像背景色2aa672
           /* viewHolder.music1.setBackgroundColor(Color.parseColor("#2aa672"));
            viewHolder.music2.setBackgroundColor(Color.parseColor("#2aa672"));
            viewHolder.music3.setBackgroundColor(Color.parseColor("#2aa672"));*/

        }else{
            //主背景色5ba3d5
            viewHolder.linearLayout.setBackgroundResource(R.drawable.linearlayout_shape1);
            //播放按钮背景
            viewHolder.playBackground.setBackgroundColor(Color.parseColor("#4b80a8"));
            //音乐图像背景色4a81aa
           /* viewHolder.music1.setBackgroundColor(Color.parseColor("#4a81aa"));
            viewHolder.music2.setBackgroundColor(Color.parseColor("#4a81aa"));
            viewHolder.music3.setBackgroundColor(Color.parseColor("#4a81aa"));*/

        }

        viewHolder.name.setText(channel.getUserName());
        viewHolder.time.setText(channel.getTime());
        viewHolder.content.setText(channel.getContent());

       // viewHolder.count.setText(channel.getCount());
        if(WhiteNoisePlayerService.getPlayState() == WhiteNoisePlayerService.PLAYING && WhiteNoisePlayerService.getChannel().equals("SleepFriendCreation") && WhiteNoisePlayerService.getChannelInfoId().equals(channel.getId())) {
            viewHolder.play.setBackgroundResource(R.mipmap.play2);
        }else{
            viewHolder.play.setBackgroundResource(R.mipmap.play);
        }
        viewHolder.play.setOnClickListener(this);
        viewHolder.play.setTag(position);

        return view;
    }



}
